package fun._6742.controller;

import fun._6742.request.CreateProductRequest;
import fun._6742.request.UpdateProductRequest;
import fun._6742.service.AdminProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = "商品管理")
@RestController
@RequestMapping("api/admin/product/v1")
public class AdminProductController {

    @Resource
    AdminProductService adminProductService;

    @ApiOperation(value = "商品新增", notes = "商品图片传base64字符串")
    @PostMapping("/")
    public void newProduct(@ApiParam(value = "商品信息") @RequestBody CreateProductRequest createProductRequest) {
        adminProductService.createProduct(createProductRequest);
    }

    @ApiOperation(value = "商品删除")
    @DeleteMapping("/{id}")
    public void delProduct(@ApiParam(value = "商品id") @PathVariable(value = "id") long id) {
        adminProductService.deleteProduct(id);
    }

    @ApiOperation(value = "商品查找", notes = "分页查询商品列表")
    @GetMapping("/")
    public Object pageProductList(
            @ApiParam(value = "id") @RequestParam(value = "id", defaultValue = "-1") long id,
            @ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "每页显示条数") @RequestParam(value = "size", defaultValue = "10") int size,
            @ApiParam(value = "类别id") @RequestParam(value = "category_id", defaultValue = "-1") int categoryId) {
        if (id > 0) {
            return adminProductService.findOne(id, categoryId);
        } else {
            return adminProductService.findPage(page, size, categoryId);
        }
    }

    @ApiOperation(value = "商品更新")
    @PutMapping("/{id}")
    public void updateProduct(@ApiParam(value = "商品id")
                              @PathVariable(value = "id") long id,
                              @ApiParam(value = "商品信息")
                              @RequestBody UpdateProductRequest updateProductRequest) {
        adminProductService.updateOneProduct(id, updateProductRequest);
    }

    @ApiOperation(value = "商品图片更新")
    @PutMapping("/{id}/updateImg")
    public void updateProductImg(@ApiParam(value = "商品id")
                                 @PathVariable(value = "id") long id,
                                 @ApiParam(value = "商品图片Base64")
                                 @RequestBody String url) {
        adminProductService.updateOneProductImg(id, url);
    }

    @ApiOperation(value = "商品新增库存")
    @PutMapping("/stock/{id}")
    public void updateStock(@ApiParam(value = "商品id")
                            @PathVariable(value = "id") long id,
                            @RequestParam(value = "stock_add_num") int stockAddNum) {
        adminProductService.updateProductStock(id, stockAddNum);
    }
}
